package org.example.smartbuy.serviceImpl;

import org.example.smartbuy.entity.Inventory;
import org.example.smartbuy.entity.Productactions;
import org.example.smartbuy.entity.Products;
import org.example.smartbuy.mapper.InventoryMapper;
import org.example.smartbuy.mapper.ProductsMapper;
import org.example.smartbuy.service.IInventoryService;
import org.example.smartbuy.service.IProductactionsService;
import org.example.smartbuy.service.IProductsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Smarty Buy
 * @since 2024-11-18
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements IProductsService {

    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private IInventoryService inventoryService;

    @Autowired
    private IProductactionsService productactionsService;

    @Override
    public List<Products> listAll() {
        return productsMapper.selectList(null);
    }

    @Override
    public Products add(Products products, int amount, int adminId) {
        //新增商品的同时
        productsMapper.insert(products);
        //增加库存项
        Inventory inventory = new Inventory(null,products.getProductId(),amount,null);
        inventoryService.add(inventory);
        //操作记录
        Productactions productactions = new Productactions(null,adminId, products.getProductId(), "add",null, "新增商品，商品名称：" + products.getName() + "，数量：" + amount);
        productactionsService.add(productactions);

        return products;
    }

    @Override
    public  Products getById(int id) {
        return productsMapper.selectById(id);
    }

    @Override
    public Products update(Products products, int adminId) {
        productsMapper.updateById(products);

        //操作记录
        Productactions productactions = new Productactions(null,adminId, products.getProductId(), "update",null, "更新商品的Id：" + products.getProductId());
        productactionsService.add(productactions);

        return products;
    }

    @Override
    public void delete(int id, int adminId) {
        Products products = productsMapper.selectById(id);
        productsMapper.deleteById(id);
        //操作记录
        Productactions productactions = new Productactions(null,adminId,id,"delete",null,"删除商品的信息：" + products.getName());
        productactionsService.add(productactions);
    }
}
